﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SmartMathLibrary.Management
{
    /// <summary>
    /// All classes, which inherited from this class, get automatically the possibility to clone themselves. The inherited 
    /// classes have to marked as Serializable.
    /// </summary>
    [Serializable]
    public abstract class CloneableObject : Object, ICloneable
    {
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            MemoryStream memoryStream = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            binaryFormatter.Serialize(memoryStream, this);
            memoryStream.Position = 0;

            object result = binaryFormatter.Deserialize(memoryStream);

            memoryStream.Close();

            return result;
        }
    }
}